﻿namespace Liger.Web.UI
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using Liger.Web.Common;
    using Liger.Web.Common.Extensions;
    using System.ComponentModel;
    using Liger.Web.Javascript;
    using Liger.Web.Common.JSON;

    public class Dialog : UIComponentBase
    {

        public Dialog()
            : this(new StringBuilder())
        {

        }
        public Dialog(StringBuilder writer)
            : base(writer)
        {
            this.Properties = new Dictionary<string, object>();
        }
        public string DialogType
        {
            get;
            set;
        }
		public string Cls
        {
            get;
            set;
        }
		public string Id
        {
            get;
            set;
        }
		public string Buttons
        {
            get;
            set;
        }
		public bool? IsDrag
        {
            get;
            set;
        }
		public int? Width
        {
            get;
            set;
        }
        public int Height
        {
            get;
            set;
        }
		public string Content
        {
            get;
            set;
        }
		public string Target
        {
            get;
            set;
        }
		public string Url
        {
            get;
            set;
        }
		public bool? Load
        {
            get;
            set;
        }
		public string Type
        {
            get;
            set;
        }
		public string Left
        {
            get;
            set;
        }
		public string Top
        {
            get;
            set;
        }
		public bool? Modal
        {
            get;
            set;
        }
		public string Name
        {
            get;
            set;
        }
		public bool? IsResize
        {
            get;
            set;
        }
		public bool? AllowClose
        {
            get;
            set;
        }
		public string Opener
        {
            get;
            set;
        }
		public string TimeParmName
        {
            get;
            set;
        }
		public string CloseWhenEnter
        {
            get;
            set;
        }
		public bool? IsHidden
        {
            get;
            set;
        }
		public bool? Show
        {
            get;
            set;
        }
		public string Title
        {
            get;
            set;
        }
		public bool? ShowMax
        {
            get;
            set;
        }
		public bool? ShowToggle
        {
            get;
            set;
        }
		public bool? ShowMin
        {
            get;
            set;
        }
		public bool? Slide
        {
            get;
            set;
        }
		public string FixedType
        {
            get;
            set;
        }
		public string ShowType
        {
            get;
            set;
        }

        /// <summary>
        /// 其他可扩展的属性
        /// </summary>
        public Dictionary<string, object> Properties
        {
            get;
            private set;
        }

        /// <summary>
        /// 返回ligerDialog的Manager
        /// </summary>
        public string Manager
        {
            get;
            set;
        }

        public override void WriteHTML()
        {
            if (!ID.HasValue())
                ID = "Dialog" + System.Guid.NewGuid().ToString();

            string TagName = string.IsNullOrEmpty(this.TagName) ? "div" : this.TagName;

            Writer.Append("<{1} id='{0}'></{1}>".FormatWith(ID, TagName));

            base.WriteScript();
        }
        public override void WriteScript()
        {
            //JSWriter.WriteJQueryOnLoad();
            if (this.Manager.HasValue())
            {
                JSWriter.Append("window.{0} = ".FormatWith(this.Manager));
            }
            JSWriter.Append("jQuery.ligerDialog("); 
            JSWriter.Append(GetDialogParmJSON());
            JSWriter.Append(");");
            //JSWriter.WriteEndJQueryOnLoad();

            base.WriteScript();
        }

        public string GetDialogParmJSON()
        {
            IJavascriptObjectWriter JSWriter = new JavascriptObjectWriter();
            JSWriter.PropertyVariableCase = this.JSWriter.PropertyVariableCase;
            JSWriter.WriteOjbect(); 
			JSWriter.WriteProperty("Cls", this.Cls); 
			JSWriter.WriteProperty("Id", this.Id); 
			JSWriter.WriteProperty("Buttons", this.Buttons); 
			JSWriter.WriteProperty("IsDrag", this.IsDrag); 
			JSWriter.WriteProperty("Width", this.Width); 
			JSWriter.WriteProperty("Height", this.Height); 
			JSWriter.WriteProperty("Content", this.Content); 
			JSWriter.WriteProperty("Target", this.Target); 
			JSWriter.WriteProperty("Url", this.Url); 
			JSWriter.WriteProperty("Load", this.Load); 
			JSWriter.WriteProperty("Type", this.Type); 
			JSWriter.WriteProperty("Left", this.Left); 
			JSWriter.WriteProperty("Top", this.Top); 
			JSWriter.WriteProperty("Modal", this.Modal); 
			JSWriter.WriteProperty("Name", this.Name); 
			JSWriter.WriteProperty("IsResize", this.IsResize); 
			JSWriter.WriteProperty("AllowClose", this.AllowClose); 
			JSWriter.WriteProperty("Opener", this.Opener); 
			JSWriter.WriteProperty("TimeParmName", this.TimeParmName); 
			JSWriter.WriteProperty("CloseWhenEnter", this.CloseWhenEnter); 
			JSWriter.WriteProperty("IsHidden", this.IsHidden); 
			JSWriter.WriteProperty("Show", this.Show); 
			JSWriter.WriteProperty("Title", this.Title); 
			JSWriter.WriteProperty("ShowMax", this.ShowMax); 
			JSWriter.WriteProperty("ShowToggle", this.ShowToggle); 
			JSWriter.WriteProperty("ShowMin", this.ShowMin); 
			JSWriter.WriteProperty("Slide", this.Slide); 
			JSWriter.WriteProperty("FixedType", this.FixedType); 
			JSWriter.WriteProperty("ShowType", this.ShowType); 
            foreach (var prop in this.Properties.Keys)
            {
                var value = this.Properties[prop];
                JSWriter.WritePropertyObject(prop, value);
            } 
            foreach (var even in this.Events)
            {
                JSWriter.WriteProperty(even.Key, even.Value);
            } 
            JSWriter.WriteEndOjbect(); 
            return JSWriter.Output();
        }
    }
}